﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DocClient
{
    public partial class SelectFileForm : Form
    {
        private DoctorForm _form;
        public delegate void RefreshListDelegate(string[] fileNames);

        public SelectFileForm(DoctorForm f)
        {
            InitializeComponent();
            this._form = f;
            openFileButton.Enabled = false;
        }

        public void AcquireLogs()
        {
            _form.secureConnection.SendObject("getSavedFilesList");
        }

        public void RefreshList(string[] fileNames)
        {
            if (FileListBox.InvokeRequired)
            {
                Invoke(new RefreshListDelegate(RefreshList), new object[] { fileNames });
                return;
            }

            foreach (string s in fileNames)
            {
                FileListBox.Items.Add(s);
            }

            if (FileListBox.Items.Count > 0)
                openFileButton.Enabled = true;
        }

        private void OpenLog_Click(object sender, EventArgs e)
        {
            object selected = FileListBox.SelectedItem;
            if (selected != null)
            {
                _form.secureConnection.SendObject("getSavedFile:" + selected as string);
            }
            
            this.Hide();
        }
    }
}
